/*
 * ConsultarResiduosMedicos.java
 *
 * Created on 09-may-2012, 15:55:53
 */

package interfazgrafica.consultar.residuosMedicos;

import interfazgrafica.ayuda.VentanaAyuda;
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.logging.Level;
import java.util.logging.Logger;
import java.sql.Statement;
import javax.swing.table.DefaultTableModel;

/**
 *
 * @author Lauaa
 */
public class ConsultarResiduosMedicos extends javax.swing.JFrame {
    private final Connection conn;
    private final Statement stmt;

    /** Creates new form ConsultarResiduosMedicos */
    public ConsultarResiduosMedicos() throws ClassNotFoundException, SQLException {
        initComponents();
        //Accedo a la base de datos.
            Class.forName("oracle.jdbc.OracleDriver");
            conn = DriverManager.getConnection("jdbc:oracle:thin:@//localhost:1521/XE", "system", "terrano89");
            stmt = conn.createStatement();

        //Hago una consulta y la guardo en rs.
	ResultSet rs = null;
            rs = stmt.executeQuery("SELECT fecharetirada, gestor, id, observaciones FROM ResiduosMedicos");

                DefaultTableModel modelo = new DefaultTableModel();
                jTable1.setModel(modelo);

                // Creamos las columnas.
                modelo.addColumn("Fecha Retirada");
                modelo.addColumn("Gestor");
                modelo.addColumn("Id");
                modelo.addColumn("Observaciones");

                // Bucle para cada resultado en la consulta
                while (rs.next())
                {
                   // Se crea un array que será una de las filas de la tabla.
                   Object [] fila = new Object[4]; // Hay tres columnas en la tabla

                   // Se rellena cada posición del array con una de las columnas de la tabla en base de datos.
                   for (int i=0;i<4;i++)
                      fila[i] = rs.getObject(i+1); // El primer indice en rs es el 1, no el cero, por eso se suma 1.

                   // Se añade al modelo la fila completa.
                   modelo.addRow(fila);
                }
                jTable1.setModel(modelo);

            rs.close();
    }

    /** This method is called from within the constructor to
     * initialize the form.
     * WARNING: Do NOT modify this code. The content of this method is
     * always regenerated by the Form Editor.
     */
    @SuppressWarnings("unchecked")
    // <editor-fold defaultstate="collapsed" desc="Generated Code">//GEN-BEGIN:initComponents
    private void initComponents() {

        jScrollPane1 = new javax.swing.JScrollPane();
        jTable1 = new javax.swing.JTable();
        jLabelId = new javax.swing.JLabel();
        jLabelGestor = new javax.swing.JLabel();
        jLabelFechaRetirada = new javax.swing.JLabel();
        jTextFieldGestor = new javax.swing.JTextField();
        jTextFieldFechaRetirada = new javax.swing.JTextField();
        jTextFieldObservaciones = new javax.swing.JTextField();
        jLabelNumeroAnimales = new javax.swing.JLabel();
        jTextFieldId = new javax.swing.JTextField();
        jButtonAyuda = new javax.swing.JButton();

        setName("Form"); // NOI18N

        jScrollPane1.setName("jScrollPane1"); // NOI18N

        jTable1.setModel(new javax.swing.table.DefaultTableModel(
            new Object [][] {

            },
            new String [] {

            }
        ));
        jTable1.setName("jTable1"); // NOI18N
        jTable1.addMouseListener(new java.awt.event.MouseAdapter() {
            public void mouseClicked(java.awt.event.MouseEvent evt) {
                jTable1MouseClicked(evt);
            }
        });
        jScrollPane1.setViewportView(jTable1);

        org.jdesktop.application.ResourceMap resourceMap = org.jdesktop.application.Application.getInstance(cowmanager.CowManagerApp.class).getContext().getResourceMap(ConsultarResiduosMedicos.class);
        jLabelId.setText(resourceMap.getString("jLabelId.text")); // NOI18N
        jLabelId.setName("jLabelId"); // NOI18N

        jLabelGestor.setText(resourceMap.getString("jLabelGestor.text")); // NOI18N
        jLabelGestor.setName("jLabelGestor"); // NOI18N

        jLabelFechaRetirada.setText(resourceMap.getString("jLabelFechaRetirada.text")); // NOI18N
        jLabelFechaRetirada.setName("jLabelFechaRetirada"); // NOI18N

        jTextFieldGestor.setName("jTextFieldGestor"); // NOI18N

        jTextFieldFechaRetirada.setName("jTextFieldFechaRetirada"); // NOI18N

        jTextFieldObservaciones.setName("jTextFieldObservaciones"); // NOI18N

        jLabelNumeroAnimales.setText(resourceMap.getString("jLabelNumeroAnimales.text")); // NOI18N
        jLabelNumeroAnimales.setName("jLabelNumeroAnimales"); // NOI18N

        jTextFieldId.setName("jTextFieldId"); // NOI18N

        jButtonAyuda.setText(resourceMap.getString("jButtonAyuda.text")); // NOI18N
        jButtonAyuda.setName("jButtonAyuda"); // NOI18N
        jButtonAyuda.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                jButtonAyudaActionPerformed(evt);
            }
        });

        javax.swing.GroupLayout layout = new javax.swing.GroupLayout(getContentPane());
        getContentPane().setLayout(layout);
        layout.setHorizontalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(javax.swing.GroupLayout.Alignment.TRAILING, layout.createSequentialGroup()
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.TRAILING)
                    .addGroup(layout.createSequentialGroup()
                        .addContainerGap()
                        .addComponent(jButtonAyuda, javax.swing.GroupLayout.PREFERRED_SIZE, 80, javax.swing.GroupLayout.PREFERRED_SIZE))
                    .addGroup(layout.createSequentialGroup()
                        .addGap(23, 23, 23)
                        .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                            .addGroup(layout.createSequentialGroup()
                                .addComponent(jLabelGestor)
                                .addGap(18, 18, 18)
                                .addComponent(jTextFieldGestor, javax.swing.GroupLayout.PREFERRED_SIZE, 136, javax.swing.GroupLayout.PREFERRED_SIZE)
                                .addGap(18, 18, 18)
                                .addComponent(jLabelNumeroAnimales))
                            .addGroup(layout.createSequentialGroup()
                                .addComponent(jLabelFechaRetirada)
                                .addGap(18, 18, 18)
                                .addComponent(jTextFieldFechaRetirada, javax.swing.GroupLayout.PREFERRED_SIZE, 143, javax.swing.GroupLayout.PREFERRED_SIZE)
                                .addGap(18, 18, 18)
                                .addComponent(jLabelId, javax.swing.GroupLayout.PREFERRED_SIZE, 23, javax.swing.GroupLayout.PREFERRED_SIZE)))
                        .addGap(18, 18, 18)
                        .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                            .addComponent(jTextFieldId, javax.swing.GroupLayout.PREFERRED_SIZE, 213, javax.swing.GroupLayout.PREFERRED_SIZE)
                            .addComponent(jTextFieldObservaciones, javax.swing.GroupLayout.DEFAULT_SIZE, 241, Short.MAX_VALUE))))
                .addContainerGap())
            .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                .addGroup(layout.createSequentialGroup()
                    .addContainerGap()
                    .addComponent(jScrollPane1, javax.swing.GroupLayout.DEFAULT_SIZE, 547, Short.MAX_VALUE)
                    .addContainerGap()))
            .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                .addGap(0, 567, Short.MAX_VALUE))
        );
        layout.setVerticalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(javax.swing.GroupLayout.Alignment.TRAILING, layout.createSequentialGroup()
                .addContainerGap(310, Short.MAX_VALUE)
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(jLabelFechaRetirada)
                    .addComponent(jTextFieldFechaRetirada, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                    .addComponent(jTextFieldId, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                    .addComponent(jLabelId))
                .addGap(18, 18, 18)
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(jLabelGestor)
                    .addComponent(jTextFieldGestor, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                    .addComponent(jLabelNumeroAnimales)
                    .addComponent(jTextFieldObservaciones, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
                .addGap(16, 16, 16)
                .addComponent(jButtonAyuda)
                .addContainerGap())
            .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                .addGroup(layout.createSequentialGroup()
                    .addGap(27, 27, 27)
                    .addComponent(jScrollPane1, javax.swing.GroupLayout.PREFERRED_SIZE, 246, javax.swing.GroupLayout.PREFERRED_SIZE)
                    .addContainerGap(145, Short.MAX_VALUE)))
            .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                .addGap(0, 418, Short.MAX_VALUE))
        );

        pack();
    }// </editor-fold>//GEN-END:initComponents

    private void jTable1MouseClicked(java.awt.event.MouseEvent evt) {//GEN-FIRST:event_jTable1MouseClicked
        try {
            //Hago una consulta y la guardo en rs2.
            ResultSet rs2 = null;
            String consulta = "SELECT * FROM residuosmedicos WHERE fecharetirada = '";
            String value = jTable1.getValueAt(jTable1.getSelectedRow(), 0).toString();
            value = (String) value.subSequence(0, 10);
            value = value.replace("-", " ");
            String[] separado = value.split(" ");
            value = separado[2]+"-"+separado[1]+"-"+separado[0];
            consulta = consulta + value + "'";
            rs2 = stmt.executeQuery(consulta);
            while (rs2.next()) {
                jTextFieldFechaRetirada.setText(rs2.getString("fecharetirada"));
                jTextFieldId.setText(rs2.getString("id"));
                jTextFieldObservaciones.setText(rs2.getString("observaciones"));
                jTextFieldGestor.setText(rs2.getString("gestor"));
            }
            rs2.close();
        } catch (SQLException ex) {
            Logger.getLogger(ConsultarResiduosMedicos.class.getName()).log(Level.SEVERE, null, ex);
        }
}//GEN-LAST:event_jTable1MouseClicked

    private void jButtonAyudaActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_jButtonAyudaActionPerformed
        VentanaAyuda ayuda = new VentanaAyuda("Consultar residuos médicos");
        ayuda.setVisible(true);
    }//GEN-LAST:event_jButtonAyudaActionPerformed

    /**
    * @param args the command line arguments
    */
    public static void main(String args[]) {
        java.awt.EventQueue.invokeLater(new Runnable() {
            public void run() {
                try {
                    new ConsultarResiduosMedicos().setVisible(true);
                } catch (ClassNotFoundException ex) {
                    Logger.getLogger(ConsultarResiduosMedicos.class.getName()).log(Level.SEVERE, null, ex);
                } catch (SQLException ex) {
                    Logger.getLogger(ConsultarResiduosMedicos.class.getName()).log(Level.SEVERE, null, ex);
                }
            }
        });
    }

    // Variables declaration - do not modify//GEN-BEGIN:variables
    private javax.swing.JButton jButtonAyuda;
    private javax.swing.JLabel jLabelFechaRetirada;
    private javax.swing.JLabel jLabelGestor;
    private javax.swing.JLabel jLabelId;
    private javax.swing.JLabel jLabelNumeroAnimales;
    private javax.swing.JScrollPane jScrollPane1;
    private javax.swing.JTable jTable1;
    private javax.swing.JTextField jTextFieldFechaRetirada;
    private javax.swing.JTextField jTextFieldGestor;
    private javax.swing.JTextField jTextFieldId;
    private javax.swing.JTextField jTextFieldObservaciones;
    // End of variables declaration//GEN-END:variables

}
